<template>
  <el-dialog
    :title="!dataForm.laId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="laUserId">
      <el-input v-model="dataForm.laUserId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="小说id" prop="laBookId">
      <el-input v-model="dataForm.laBookId" placeholder="小说id"></el-input>
    </el-form-item>
    <el-form-item label="目录id" prop="laCatalogueId">
      <el-input v-model="dataForm.laCatalogueId" placeholder="目录id"></el-input>
    </el-form-item>
    <el-form-item label="小说内容" prop="laBookContentId">
      <el-input v-model="dataForm.laBookContentId" placeholder="小说内容"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="laCreationTime">
      <el-input v-model="dataForm.laCreationTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="laUpdateTime">
      <el-input v-model="dataForm.laUpdateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="评论状态" prop="laStatus">
      <el-input v-model="dataForm.laStatus" placeholder="评论状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          laId: 0,
          laUserId: '',
          laBookId: '',
          laCatalogueId: '',
          laBookContentId: '',
          laCreationTime: '',
          laUpdateTime: '',
          laStatus: ''
        },
        dataRule: {
          laUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          laBookId: [
            { required: true, message: '小说id不能为空', trigger: 'blur' }
          ],
          laCatalogueId: [
            { required: true, message: '目录id不能为空', trigger: 'blur' }
          ],
          laBookContentId: [
            { required: true, message: '小说内容不能为空', trigger: 'blur' }
          ],
          laCreationTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          laUpdateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          laStatus: [
            { required: true, message: '评论状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.laId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.laId) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tblabel/info/${this.dataForm.laId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.laUserId = data.tbLabel.laUserId
                this.dataForm.laBookId = data.tbLabel.laBookId
                this.dataForm.laCatalogueId = data.tbLabel.laCatalogueId
                this.dataForm.laBookContentId = data.tbLabel.laBookContentId
                this.dataForm.laCreationTime = data.tbLabel.laCreationTime
                this.dataForm.laUpdateTime = data.tbLabel.laUpdateTime
                this.dataForm.laStatus = data.tbLabel.laStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fiction/tblabel/${!this.dataForm.laId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'laId': this.dataForm.laId || undefined,
                'laUserId': this.dataForm.laUserId,
                'laBookId': this.dataForm.laBookId,
                'laCatalogueId': this.dataForm.laCatalogueId,
                'laBookContentId': this.dataForm.laBookContentId,
                'laCreationTime': this.dataForm.laCreationTime,
                'laUpdateTime': this.dataForm.laUpdateTime,
                'laStatus': this.dataForm.laStatus
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
